const debug = require('debug')('rlcas:logger')
const os = require('os');
const logFormat = require('./format');
const kafka = require('./kafka')
const logfile = require('./logfile');
const hostname = os.hostname();
const messageNum = require('./messagenum');
const logts = ['click','pu','app'];
/**
 * 追加数据到底部
 * 
 * @param {*} id 
 * @param {*} data 
 * @param {*} ip 
 */
function logData(id, data, ip) {
  data.moonid = id;
  const str = logFormat(ip, data);
  debug('logData:' + str);
  data.t = data.t || 'click';
  if (logts.includes(data.t)) kafka.log([str]);
  else logfile(data)
  messageNum.setNum(1);
  //  logfile(data);
}

/**
 * 追加数据到底部
 * 
 * @param {*} id 
 * @param {*} datas
 * @param {*} ip 
 */
function logDatas(id, data, ip) {
  try {
    const _element = []
    data.xt_logdata.forEach(element => {
      element.moonid = id;
      element.t = element.t || 'click';
      if (logts.includes(element.t)) _element.push(logFormat(ip, element));
      else logfile(element)
    });
    messageNum.setNum(_element.length);
    debug('logDatas: length=' + _element.length)
    kafka.log(_element);
  } catch (e) {
    console.error(e);
  }
}
/**
 * 追加数据到底部
 * 
 * @param {*} id 
 * @param {*} data 
 * @param {*} ip 
 */
function logdebug(id, data, ip) {
  try {
    const _element = []
    data.xt_logdata.forEach(element => {
      element.moonid = id;
      _element.push(logFormat(ip, element));
      logfile(element)
    });
    //  kafka.debug(_element)
  } catch (e) {
    console.error(e);
  }
  // data.moonid = id;
  // data.moonhostname = hostname;
  // const str = logFormat(ip, data);
  // debug('logData:'+ str);
  // kafka.debug([str]);
  // logfile(data);
}

module.exports.logData = logData;
module.exports.logDatas = logDatas;
module.exports.logdebug = logdebug;